#!/bin/sh

# Determine the path to this application.
CURDIR="`pwd`"
APPDIR=`dirname "$0"`
cd "${APPDIR}"
APPDIR="`pwd`"
cd "${CURDIR}"

# Get translations
ENVIRONMENT_LANG=`echo $LANG | sed "s/\(..\).*/\1/"` #detect environment language 
case $ENVIRONMENT_LANG in
	de)	export LANGUAGE="german" ;;
	en)	export LANGUAGE="english" ;;
	es)	export LANGUAGE="spanish" ;;
	fr)	export LANGUAGE="french" ;;
	it)	export LANGUAGE="italian" ;;
	nb)	export LANGUAGE="norwegian" ;; #translation is norwegian bokmål
	nl)	export LANGUAGE="dutch" ;;
	nn)	export LANGUAGE="norwegian" ;; #Use norwegian bokmål, since there is no norwegian nynorsk
	ru)	export LANGUAGE="russian" ;;
	*)	export LANGUAGE="english" ;; #english for contries without translation
esac
. $APPDIR/locale/english.po #always run to fill gaps in translation
[ -f "$APPDIR/locale/${LANGUAGE}.po" ] && . $APPDIR/locale/${LANGUAGE}.po 2> /dev/null


# set Xterm colors
case $1 in
 -colors)
 # get the current colors
 CURRENTBG="`cat $HOME/.Xresources | grep 'XTerm' | grep 'background' | cut -f 2 -d ' '`"
 CURRENTFG="`cat $HOME/.Xresources | grep 'XTerm' | grep 'foreground' | cut -f 2 -d ' '`"
 echo $CURRENTBG > $HOME/.config/tmp/xtermbg
 echo $CURRENTFG > $HOME/.config/tmp/xtermfg
 
 # transparency
 if [ ! "`cat $HOME/.Xdefaults | grep Rxvt | grep inheritPixmap| grep true`" = "" ];then
  TRANS="true"
  VIS="disabled"
 else
  TRANS="false"
  VIS="enabled"
 fi
 
 # font size
 FNT="`cat $HOME/.Xresources | grep 'XTerm' | grep 'font' | cut -f 2 -d ' '`"
 case $FNT in
  6x10)
  TNY="<default>true</default>"
  ;;
  6x13)
  SMA="<default>true</default>"
  ;;
  7x14)
  NML="<default>true</default>"
  ;;
  9x15)
  LRG="<default>true</default>"
  ;;
  10x20)
  HUG="<default>true</default>"
  ;;
 esac
 
 # create the dialog window
 export COLORS_DLG="
 <window title=\"$LOC_MAIN\" icon_name=\"gtk-select-color\">
  <vbox>
   <text use-markup=\"true\">
    <label>\"<b>$LOC_MAIN_HINT</b>\"</label>
   </text>
   <hbox>
    <vbox>
     <frame $LOC_FOREGND>
      <hbox>
       <vbox>
        <hbox>
         <entry editable=\"false\">
          <variable>FG</variable>
          <input>cat $HOME/.config/tmp/xtermfg</input>
         </entry>
        </hbox>
       </vbox>
       <vbox>
        <hbox>
         <button>
          <input file>\"$APPDIR/images/white.png\"</input>
          <action>echo White > $HOME/.config/tmp/xtermfg</action>
          <action type=\"refresh\">FG</action>
         </button>
         <button>
          <input file>\"$APPDIR/images/antiquewhite.png\"</input>
          <action>echo AntiqueWhite > $HOME/.config/tmp/xtermfg</action>
          <action type=\"refresh\">FG</action>
         </button>
         <button>
          <input file>\"$APPDIR/images/grey25.png\"</input>
          <action>echo Grey25 > $HOME/.config/tmp/xtermfg</action>
          <action type=\"refresh\">FG</action>
         </button>
         <button>
          <input file>\"$APPDIR/images/black.png\"</input>
          <action>echo Black > $HOME/.config/tmp/xtermfg</action>
          <action type=\"refresh\">FG</action>
         </button>
         <button>
          <input file>\"$APPDIR/images/magenta.png\"</input>
          <action>echo Magenta > $HOME/.config/tmp/xtermfg</action>
          <action type=\"refresh\">FG</action>
         </button>
        </hbox>
        <hbox>
         <button>
          <input file>\"$APPDIR/images/red.png\"</input>
          <action>echo Red > $HOME/.config/tmp/xtermfg</action>
          <action type=\"refresh\">FG</action>
         </button>
         <button>
          <input file>\"$APPDIR/images/green.png\"</input>
          <action>echo Green > $HOME/.config/tmp/xtermfg</action>
          <action type=\"refresh\">FG</action>
         </button>
         <button>
          <input file>\"$APPDIR/images/yellow.png\"</input>
          <action>echo Yellow > $HOME/.config/tmp/xtermfg</action>
          <action type=\"refresh\">FG</action>
         </button>
         <button>
          <input file>\"$APPDIR/images/blue.png\"</input>
          <action>echo Blue > $HOME/.config/tmp/xtermfg</action>
          <action type=\"refresh\">FG</action>
         </button>
         <button>
          <input file>\"$APPDIR/images/cyan.png\"</input>
          <action>echo Cyan > $HOME/.config/tmp/xtermfg</action>
          <action type=\"refresh\">FG</action>
         </button>
        </hbox>
       </vbox>
      </hbox>
     </frame>
     <frame $LOC_BACKGND>
      <hbox>
       <vbox>
        <hbox>
         <entry editable=\"false\">
          <visible>$VIS</visible>
          <input>cat $HOME/.config/tmp/xtermbg</input>
          <variable>BG</variable>
         </entry>
        </hbox>
        <hbox>
         <checkbox>
          <label>$LOC_TRANS</label>
          <default>$TRANS</default>
          <variable>TRANS</variable>
          <action>if true disable:BG</action>
          <action>if false enable:BG</action>
          <action>if true disable:WH</action>
          <action>if false enable:WH</action>
          <action>if true disable:AW</action>
          <action>if false enable:AW</action>
          <action>if true disable:GY</action>
          <action>if false enable:GY</action>
          <action>if true disable:BK</action>
          <action>if false enable:BK</action>
          <action>if true disable:MG</action>
          <action>if false enable:MG</action>
          <action>if true disable:RD</action>
          <action>if false enable:RD</action>
          <action>if true disable:GN</action>
          <action>if false enable:GN</action>
          <action>if true disable:YW</action>
          <action>if false enable:YW</action>
          <action>if true disable:BU</action>
          <action>if false enable:BU</action>
          <action>if true disable:CN</action>
          <action>if false enable:CN</action>
         </checkbox>
        </hbox>
       </vbox>
       <vbox>
        <hbox>
         <button>
          <input file>\"$APPDIR/images/white.png\"</input>
          <visible>$VIS</visible>
          <variable>WH</variable>
          <action>echo White > $HOME/.config/tmp/xtermbg</action>
          <action type=\"refresh\">BG</action>
         </button>
         <button>
          <input file>\"$APPDIR/images/antiquewhite.png\"</input>
          <visible>$VIS</visible>
          <variable>AW</variable>
          <action>echo AntiqueWhite > $HOME/.config/tmp/xtermbg</action>
          <action type=\"refresh\">BG</action>
         </button>
         <button>
          <input file>\"$APPDIR/images/grey25.png\"</input>
          <visible>$VIS</visible>
          <variable>GY</variable>
          <action>echo Grey25 > $HOME/.config/tmp/xtermbg</action>
          <action type=\"refresh\">BG</action>
         </button>
         <button>
          <input file>\"$APPDIR/images/black.png\"</input>
          <visible>$VIS</visible>
          <variable>BK</variable>
          <action>echo Black > $HOME/.config/tmp/xtermbg</action>
          <action type=\"refresh\">BG</action>
         </button>
         <button>
          <input file>\"$APPDIR/images/magenta.png\"</input>
          <visible>$VIS</visible>
          <variable>MG</variable>
          <action>echo Magenta > $HOME/.config/tmp/xtermbg</action>
          <action type=\"refresh\">BG</action>
         </button>
        </hbox>
        <hbox>
         <button>
          <input file>\"$APPDIR/images/red.png\"</input>
          <visible>$VIS</visible>
          <variable>RD</variable>
          <action>echo Red > $HOME/.config/tmp/xtermbg</action>
          <action type=\"refresh\">BG</action>
         </button>
         <button>
          <input file>\"$APPDIR/images/green.png\"</input>
          <visible>$VIS</visible>
          <variable>GN</variable>
          <action>echo Green > $HOME/.config/tmp/xtermbg</action>
          <action type=\"refresh\">BG</action>
         </button>
         <button>
          <input file>\"$APPDIR/images/yellow.png\"</input>
          <visible>$VIS</visible>
          <variable>YW</variable>
          <action>echo Yellow > $HOME/.config/tmp/xtermbg</action>
          <action type=\"refresh\">BG</action>
         </button>
         <button>
          <input file>\"$APPDIR/images/blue.png\"</input>
          <visible>$VIS</visible>
          <variable>BU</variable>
          <action>echo Blue > $HOME/.config/tmp/xtermbg</action>
          <action type=\"refresh\">BG</action>
         </button>
         <button>
          <input file>\"$APPDIR/images/cyan.png\"</input>
          <visible>$VIS</visible>
          <variable>CN</variable>
          <action>echo Cyan > $HOME/.config/tmp/xtermbg</action> 
          <action type=\"refresh\">BG</action>
         </button>
        </hbox>
       </vbox>
      </hbox>
     </frame>
    </vbox>
    <frame $LOC_FN>
     <radiobutton>
      <label>$LOC_TINY</label>
      $TNY
      <variable>TINY</variable>
     </radiobutton>
     <radiobutton>
      <label>$LOC_SMALL</label>
      $SMA
      <variable>SMALL</variable>
     </radiobutton>
     <radiobutton>
      <label>$LOC_NORMAL</label>
      $NML
      <variable>NORMAL</variable>
     </radiobutton>
     <radiobutton>
      <label>$LOC_LARGE</label>
      $LRG
      <variable>LARGE</variable>
     </radiobutton>
     <radiobutton>
      <label>$LOC_HUGE</label>
      $HUG
      <variable>HUGE</variable>
     </radiobutton>
    </frame>
   </hbox>
   <hbox>
    <button cancel>
     <action>EXIT:abort</action>
    </button>
    <button ok></button>
   </hbox>
  </vbox>
 </window>
 "
 gtkdialog3 --program COLORS_DLG > $HOME/.config/tmp/xtermcolors
 . $HOME/.config/tmp/xtermcolors
 rm -f $HOME/.config/tmp/*
 [ "$EXIT" = "abort" ] && exit
 
 # font size
 [ "$TINY" = "true" ] && FN="6x10"
 [ "$SMALL" = "true" ] && FN="6x13"
 [ "$NORMAL" = "true" ] && FN="7x14"
 [ "$LARGE" = "true" ] && FN="9x15"
 [ "$HUGE" = "true" ] && FN="10x20"
 
 # replace the settings
 cat $HOME/.Xresources | grep -v 'XTerm\*foreground' | grep -v 'XTerm\*background' | grep -v "XTerm\*font" | grep -v "Rxvt\*inheritPixmap" > $HOME/.Xresources.new
 cat $HOME/.Xdefaults | grep -v 'XTerm\*foreground' | grep -v 'XTerm\*background' | grep -v "XTerm\*font" | grep -v "Rxvt\*inheritPixmap" > $HOME/.Xdefaults.new
 echo "XTerm*foreground: $FG" >> $HOME/.Xresources.new
 echo "XTerm*foreground: $FG" >> $HOME/.Xdefaults.new
 echo "XTerm*background: $BG" >> $HOME/.Xresources.new
 echo "XTerm*background: $BG" >> $HOME/.Xdefaults.new
 echo "XTerm*font: $FN" >> $HOME/.Xresources.new
 echo "XTerm*font: $FN" >> $HOME/.Xdefaults.new
 echo "Rxvt*inheritPixmap: $TRANS" >> $HOME/.Xresources.new
 echo "Rxvt*inheritPixmap: $TRANS" >> $HOME/.Xdefaults.new
 mv -f $HOME/.Xresources.new $HOME/.Xresources
 mv -f $HOME/.Xdefaults.new $HOME/.Xdefaults
 exit
 ;;
 -default)
 . $HOME/.config/defaultprograms.txt
 echo $TERMINAL > $HOME/.config/tmp/term
 cat $HOME/.config/defaultprograms.txt | grep -v TERMINAL > $HOME/.config/defaultprograms.new
 programchooser $TERMINAL utilities-terminal TerminalEmulator NONE > $HOME/.config/tmp/terminal
 TERMINAL="`cat $HOME/.config/tmp/terminal`"
 [ "$TERMINAL" = "" ] && TERMINAL="`cat $HOME/.config/tmp/term`"
 echo TERMINAL=\"$TERMINAL\" >> $HOME/.config/defaultprograms.new
 mv -f $HOME/.config/defaultprograms.new $HOME/.config/defaultprograms.txt
 rm -f $HOME/.config/tmp/term
 rm -f $HOME/.config/tmp/terminal
 ;;
 *)
 . $HOME/.config/defaultprograms.txt
 exec $TERMINAL -e "$@"
 ;;
esac













